// pages/detail/detail.js
var app = getApp();
const util = require("../../../../utils/util.js");
let detailId;
let toggleCollection = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        markers: [{
            latitude: 22.565000,
            longitude: 114.172490,
            name: "本地宝",
            reload: false,
            desc: "深圳本地宝有限公司"
        }],
        scroll: false,
        showPhone: false, //电话弹框
        collect: false, //收藏按钮
        detailPath: '',
        locLatitude:'',
        locLongitude:'',
        cityName:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            cityName: options.cityName
        })
        this.setData({
            locLongitude: options.lng,
            locLatitude: options.lat,
        })
        ks.setNavigationBarTitle({
            title: '网点详情',
        });
        detailId = options.detailId;
        util.getUrl(app.globalData.url).then((res) => {
            this.getDetailData();
        });
        util.location();
    },
    // 关闭电话弹框
    tapDialogButton() {
        this.setData({
            showPhone: false
        })
    },
    getDetailData: function() {
        var data = {
            action: 'service',
            id: detailId
        };
        util.request('network.php', data, 'POST').then(res => {
            var detailMsg = res.data[0];
            let mapLocation = this.bMapTransQQMap(detailMsg.com_jd, detailMsg.com_wd);
            this.setData({
                detailMsg,
                phoneArr: this.mobilePhoneArray(detailMsg.com_tel),
                markers: [{
                    latitude: mapLocation['lat'],
                    longitude: mapLocation['lng'],
                    name: detailMsg.com_name,
                }],
                detailPath: `https://map.qq.com/m/place/result/word=${detailMsg.com_name}&mode=map/`
            });
            toggleCollection = true;
        })
    },
    addrDetail: function() {
        if (this.data.detailMsg.com_wd && this.data.detailMsg.com_jd) {
            let plugin = requirePlugin('routePlan');
            let key = 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'; //使用在腾讯位置服务申请的key
            let referer = '本地宝'; //调用插件的app的名称
            let endPoint = JSON.stringify({ //终点
                'name': this.data.detailMsg.com_name,
                'latitude': this.data.markers[0].latitude,
                'longitude': this.data.markers[0].longitude
            });
            let enp = JSON.parse(endPoint)
            let lat = ks.getStorageSync('lat')
            let lng = ks.getStorageSync('lng')
            let urls = encodeURIComponent(
                `https://map.qq.com/m/index/nav/type=bus&cond=3&sword=我的位置&spointx=${lng}&spointy=${lat}&eword=${enp.name}`
                // `https://map.qq.com/m/place/result/city=${this.data.cityName}&word=${enp.name}&mode=map/`
                )
            ks.navigateTo({
                // url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint +'&navigation=1'
                url: `/pages/webview/webview?path=${urls}`
            });
        } else {
            ks.showToast({
                title: '该地点暂无导航',
                icon: 'none'
            });
        }

    },
    call: function() {
        this.setData({
            showPhone: true
        })
    },
    remarkMakePhoneCall(e) {
        let phoneNumber = util.getValue('phone', e)
        ks.makePhoneCall({
            phoneNumber
        })
    },

    /**分割出字符串中的手机号*/
    mobilePhoneArray(strContent) {
        if (!strContent) {
            return
        }
        var mobileReg = /[1][3,4,5,7,6,8,9][0-9]{9}|\d{3,4}-\d{7,8}|[0]\d{9,11}|\d{7,8}/g
        var telephoneReg
        var phoneList = strContent.match(mobileReg) || []
        var textList = strContent.split(/[1][3,4,5,7,6,8,9][0-9]{9}|\d{3,4}-\d{7,8}|[0]\d{9,11}|\d{7,8}/),
            list = []

        for (var i in textList) {
            if (textList[i] == '') {
                i == 0 && list.push({
                    type: 'phone',
                    val: phoneList[0]
                })
            } else {
                list.push({
                    type: 'text',
                    val: textList[i]
                })
                list.push({
                    type: 'phone',
                    val: phoneList[i]
                })
            }
        }
        return list
    },

    // 百度地图的经纬度转腾讯地图经纬度
    bMapTransQQMap(lng, lat) {
        let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        let x = lng - 0.0065;
        let y = lat - 0.006;
        let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        let lngs = z * Math.cos(theta);
        let lats = z * Math.sin(theta);

        return {
            lng: lngs,
            lat: lats
        }
    },
    guide() {
        ks.navigateTo({
            url: '../contactPage?guide=1',
        });
    },

    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: `本地宝-网点详情-${this.data.detailMsg.classname}-${this.data.detailMsg.com_name}`,
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            path: '/pages/tools/wangdian/detail/detail' + detailId
        }
    }
})
